<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :sm="24" :md="12" :xl="8">
            <a-form-item label="公司名称">
              <a-input allowClear placeholder="请输入" v-model="queryParam.companyName"></a-input>
            </a-form-item>
          </a-col>
          <a-col :sm="24" :md="12" :xl="8">
            <a-form-item label="公司法人">
              <a-input allowClear placeholder="请输入" v-model="queryParam.legalPerson"></a-input>
            </a-form-item>
          </a-col>
          <a-col :sm="24" :md="12" :xl="8">
            <a-form-item label="公司类别">
              <j-dict-select-tag
                allowClear
                v-model="queryParam.companyType"
                placeholder="请选择"
                dictCode="companyType"
              />
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus">
            <a-col :sm="24" :md="12" :xl="8">
              <a-form-item label="公司状态">
                <j-dict-select-tag
                  allowClear
                  v-model="queryParam.status"
                  placeholder="请选择"
                  dictCode="companyStatus"
                />
              </a-form-item>
            </a-col>
            <a-col :sm="24" :md="12" :xl="8">
              <a-form-item label="创建时间">
                <a-range-picker allowClear class="per100" v-model="tempTimes" @change="timeChange" />
              </a-form-item>
            </a-col>
          </template>

          <a-col :sm="24" :md="12" :xl="8">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button @click="searchReset()" icon="reload" style="margin-left: 8px">重置</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'" />
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <!-- table区域 -->
    <a-table
      ref="table"
      bordered
      size="middle"
      rowKey="id"
      :columns="columns"
      :dataSource="dataSource"
      :pagination="ipagination"
      :loading="loading"
      :scroll="{ x: 1200 }"
      @change="handleTableChange"
    >
      <span slot="action" slot-scope="text, record">
        <a-button type="link" class="ml5 mr5" size="small" @click="handUpdate(record.id, 'details')">详情</a-button>
        <a-button type="link" class="ml5 mr5" size="small" @click="handUpdate(record.id)">编辑</a-button>
      </span>
    </a-table>
  </a-card>
</template>

<script>
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { initDictOptions, filterDictText } from '@/components/dict/JDictSelectUtil'
import Api from '@/api/CompanyManage/CompanyManage'

export default {
  name: 'CompanyManage',
  mixins: [JeecgListMixin],
  data() {
    return {
      url: {
        list: Api.list
      },
      columns: [],
      companyTypeOptions: [],
      companyStatusOptions: []
    }
  },
  created() {
    this.columns = [
      {
        title: '序号',
        dataIndex: 'index',
        align: 'center',
        customRender: (text, record, index) => `${index + 1}`
      },
      {
        title: '公司名称',
        dataIndex: 'companyName',
        align: 'center',
        sorter: (a, b) => {
          return a.companyName - b.companyName
        }
      },
      {
        title: '公司法人',
        dataIndex: 'legalPerson',
        align: 'center',
        sorter: (a, b) => {
          return a.legalPerson - b.legalPerson
        }
      },
      {
        title: '公司地址',
        dataIndex: 'address',
        align: 'center',
        sorter: (a, b) => {
          return a.address - b.address
        }
      },
      {
        title: '工商编号',
        dataIndex: 'commercialNum',
        align: 'center',
        sorter: (a, b) => {
          return a.commercialNum - b.commercialNum
        }
      },
      {
        title: '联系人',
        dataIndex: 'contacts',
        align: 'center',
        sorter: (a, b) => {
          return a.contacts - b.contacts
        }
      },
      {
        title: '联系方式',
        dataIndex: 'contactInformation',
        align: 'center',
        sorter: (a, b) => {
          return a.contactInformation - b.contactInformation
        }
      },
      {
        title: '公司类别',
        dataIndex: 'companyType',
        align: 'center',
        sorter: (a, b) => {
          return a.companyType - b.companyType
        },
        customRender: (text, record, index) => {
          //字典值替换通用方法
          return filterDictText(this.companyTypeOptions, text)
        }
      },
      {
        title: '公司状态',
        dataIndex: 'status',
        align: 'center',
        sorter: (a, b) => {
          return a.status - b.status
        },
        customRender: (text, record, index) => {
          //字典值替换通用方法
          return filterDictText(this.companyStatusOptions, text)
        }
      },
      {
        title: '成立时间',
        dataIndex: 'establishmentTime',
        align: 'center',
        sorter: (a, b) => {
          return a.establishmentTime - b.establishmentTime
        }
      },
      {
        title: '创建时间',
        dataIndex: 'createTime',
        align: 'center',
        sorter: (a, b) => {
          return a.createTime - b.createTime
        }
      },
      {
        title: '操作',
        dataIndex: 'action',
        scopedSlots: { customRender: 'action' }
      }
    ]
    this.initDictConfig()
  },
  methods: {
    // 初始化数据字典
    initDictConfig() {
      //初始化字典 - 公司类别
      initDictOptions('companyType').then(res => {
        if (res.success) {
          this.companyTypeOptions = res.result
        }
      })
      //初始化字典 - 公司状态
      initDictOptions('companyStatus').then(res => {
        if (res.success) {
          this.companyStatusOptions = res.result
        }
      })
    },
    // 创建时间改变
    timeChange(e, arr) {
      if (arr[0]) {
        this.queryParam.startTime = arr[0]
        this.queryParam.endTime = arr[1]
      } else {
        this.queryParam.startTime = undefined
        this.queryParam.endTime = undefined
      }
    },
    // 新增/编辑网点
    handUpdate(id, details) {
      let key = id ? id : 'add'
      if (details) {
        key = `${key}?details=1`
      }
      this.$router.push(`/companyManage/companyUpdate/${key}`)
    }
  }
}
</script>

<style scoped></style>

